package com.walk.zworld.wxapi.wx;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.walk.zworld.R;
import com.walk.zworld.api.bean.UserBean;
import com.walk.zworld.api.http.CommonAPI;
import com.walk.zworld.api.http.CommonAppConfig;
import com.walk.zworld.api.http.HttpCallback;
import com.walk.zworld.api.utils.StringUtil;

import java.lang.ref.WeakReference;

/**
 * Created by  on 2019/4/22.
 */

public class PayPresenter {

    private String mWxAppID;//微信AppID
    private String mServiceNameWx;
    private long mBalanceValue;
    private Activity mActivity;
    private PayCallback mPayCallback;

    public PayPresenter(Activity activity) {
        mActivity = new WeakReference<>(activity).get();
    }

    public void setPayCallback(PayCallback callback){
        mPayCallback=callback;
    }

    public long getBalanceValue() {
        return mBalanceValue;
    }

    public void setBalanceValue(long balanceValue) {
        mBalanceValue = balanceValue;
    }

    public void setWxAppID(String wxAppID) {
        mWxAppID = wxAppID;
    }

    public void setServiceNameWx(String serviceNameWx) {
        mServiceNameWx = serviceNameWx;
    }

    public void pay(String payType, String money, String goodsName, String orderParams) {
        wxPay(orderParams);
    }


    public void pay2(String payType, String money, String goodsName,  JSONObject jsonParm,String orderId) {
        wxPay2(jsonParm);
    }

    /**
     * 微信支付
     */
    public void wxPay2(JSONObject jsonObject) {
        if (mActivity == null ) {
            return;
        }
        WxPayBuilder builder = new WxPayBuilder(mActivity, mWxAppID);
        //builder.setOrderParams(StringUtil.contact(mServiceNameWx, orderParams));
        builder.setPayCallback(mPayCallback);
        builder.pay(jsonObject);
    }

    private void wxPay( String orderParams) {
        if (mActivity == null ) {
            return;
        }
        WxPayBuilder builder = new WxPayBuilder(mActivity, mWxAppID);
        builder.setOrderParams(StringUtil.contact(mServiceNameWx, orderParams));
        builder.setPayCallback(mPayCallback);
        builder.payWx();
    }

    /**
     * 检查支付结果
     */
    public void checkPayResult() {
        CommonAPI.getBalance(new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String[] info) {
                if (code == 0 && info.length > 0) {
                    JSONObject obj = JSON.parseObject(info[0]);
                    String coin = obj.getString("coin");
                    long balanceValue = Long.parseLong(coin);
                    if (balanceValue > mBalanceValue) {
                        mBalanceValue = balanceValue;
//                        ToastUtil.show(R.string.coin_charge_success);
                        UserBean u = CommonAppConfig.getUserBean();
                        if (u != null) {
                            u.setCoin(coin);
                        }
//                        EventBus.getDefault().post(new CoinChangeEvent(coin, true));
                    }
                }
            }
        });
    }


    public void release() {
        mActivity = null;
        mPayCallback = null;
    }
}
